//
//  UIBarButtonItem+Extension.swift
//  weibomtz
//
//  Created by 毛铁柱 on 16/10/18.
//  Copyright © 2016年 mtz. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    convenience init(imgNamed:String? = nil, title:String? = nil,target: AnyObject?, action: Selector) {
        self.init()
        
        // 初始化按钮
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        if let name = imgNamed {
            //设置不同状态的图片
            button.setImage(UIImage(named: name), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "\(name)_highlighted"), forState: UIControlState.Highlighted)
        }
        
        if let _ = title {
            //设置文字及文字大小
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            button.setTitle(title, forState: UIControlState.Normal)
            //设置不同状态下的文字颜色
            button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
            button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        }
        // 内容多大控件就多大
        button.sizeToFit()
        
        customView = button
        
        
    }

}
